<?php
/**
 * @file
 */

/**
 * Theme handler for the message
 */
function theme_ajaxchatbox_message(&$variables) {
  global $user;
  $message = $variables['message'];
  $user_object = user_load($message->uid);
  // Generate user name with link
  $user_name = theme('username', array('account' => $user_object));

  // Generate title attribute
  $title = t('Posted !date at !time by !name', array('!date' => format_date($message->created, 'custom', 'm/d/y'), '!time' => format_date($message->created, 'custom', 'h:ia'), '!name' => $message->name));

  // Add to the shout classes
  $message_classes = array();
  $message_classes[] = 'ajaxchatbox-message-content';

  // Check for specific user class
  $user_classes = array();
  $user_classes[] = 'ajaxchatbox-user-name';
  if ($message->uid == $user->uid) {
    $user_classes[] = 'ajaxchatbox-current-user-name';
  }
  else if ($message->uid == 0) {
    $user_classes[] = 'ajaxchatbox-anonymous-user';
  }

  $style = array();

  if ($message->style) {
    $style_str = '';
    foreach($message->style as $key => $value) {
      $style_str .= "{$key}: {$value}; ";
    }
    if ($style_str) {
      $style = array('style' => $style_str);
    }
  }

  // Build the message
  $output = '';
  $output .= '<div id="message-' . $message->msgid . '" class="' . implode(' ', $message_classes) . '" title="' . $title . '">';
  $output .= '<span class="' . implode(' ', $user_classes) . '">' . $user_name . '</span>:&nbsp;';
  $output .= '<span class="ajaxchatbox-message" ' . drupal_attributes($style) .'>' . ajaxchatbox_smiley_filter_process($message->message, NULL, NULL) . '</span>';
  /*
  $output2 .= '<span class="ajaxchatbox-message-time">';
  $format = variable_get('ajaxchatbox_time_format', 'ago');
  switch ($format) {
    case 'ago':
      $output .=  t('!interval ago', array('!interval' => format_interval(REQUEST_TIME - $message->created)));
      break;
    case 'small':
    case 'medium':
    case 'large':
      $output .= format_date($message->created, $format);
      break;
  }
  $output2 .= '</span>';
  */
  $output .= '</div>' . "\n";
  return $output;
}

/**
 * Theme handler for the message list
 */
function theme_ajaxchatbox_messages_list(&$variables) {
  $messages = $variables['messages'];
  $output = '';
  $items = array();
  foreach ($messages as $message) {
    $items[] = array(
      'data' => theme('ajaxchatbox_message', array('message' => $message))
    );
  }

  $output .= theme('item_list', array(
      'items' => $items,
      'attributes' => array('id' => array('ajaxchatbox-message-list')))
  );
  return $output;
}

/**
 * Theme handler for the chatbox
 */
function theme_ajaxchatbox_chatbox(&$variables) {
  _ajaxchatbox_js_config();
  drupal_add_css(drupal_get_path('module', 'ajaxchatbox') . '/css/ajaxchatbox.css');
  drupal_add_library('system', 'jquery.form');
  drupal_add_js(drupal_get_path('module', 'ajaxchatbox') . '/js/ba-emotify.js');
  drupal_add_js(drupal_get_path('module', 'ajaxchatbox') . '/js/ajaxchatbox.js');

  $messages = ajaxchatbox_get_messages();
  $output = '<div id="ajaxchatbox-content">';
  $output .= theme('ajaxchatbox_messages_list', array('messages' => $messages));
  $output .= '</div>';
  $output .= '<div id="ajaxchatbox-error"></div>';

  module_load_include('inc', 'ajaxchatbox', 'ajaxchatbox.form');
  $elements = drupal_get_form('ajaxchatbox_message_form');
  $output .=  drupal_render($elements);

  return $output;
}

/**
 * Theme handler for the ajaxchatbox_message_form
 */
function theme_ajaxchatbox_message_form(&$variables) {
  $form = $variables['form'];
  $header = array();

  $form['ajaxchatbox_style']['font_family']['#title_display'] = 'invisible';
  $form['ajaxchatbox_style']['font_size']['#title_display'] = 'invisible';
  $form['ajaxchatbox_style']['color']['#title_display'] = 'invisible';

  $row = array();
  $items = array();
  $items = array(
    array(drupal_render($form['ajaxchatbox_style']['font_weight'])),
    array(drupal_render($form['ajaxchatbox_style']['font_style'])),
    array(drupal_render($form['ajaxchatbox_style']['text_decoration'])),
    array(drupal_render($form['ajaxchatbox_style']['font_family'])),
    array(drupal_render($form['ajaxchatbox_style']['color'])),
    array(drupal_render($form['smilies']))
  );
  $row[] = array(
    'data' => theme('item_list', array('items' => $items, 'attributes' => array('id' => 'ajaxchatbox-control'))),
    'colspan' => 2
  );
  $rows[] = $row;

  $row = array();
  $row[] = array('data' => drupal_render($form['ajaxchatbox_message']), 'class' => array('ajaxchatbox-message-td'));
  $row[] = array('data' => drupal_render($form['send']));
  $rows[] = $row;

  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'system-date-types')));
  $output .= drupal_render_children($form);

  return $output;
}

/**
 * Theme handler for the smileys
 */

function theme_ajaxchatbox_smileys(&$variables) {
  $output = '<div id="smiley-container">';
  $smiley_info = & drupal_static(__FUNCTION__);
  if (!isset($smiley_info)) {
    global $base_path;
    $smileys = $variables['smileys'];
    $path = $variables['path'];
    $smiley_info = '';

    foreach ($smileys as $smiley) {
      $arr = explode(' ', $smiley->acronyms);
      $smiley_info .= '<img title="' . $arr[0] . '" class="smiley" src="' . $path . '/' . $smiley->uri . '" alt="' . $smiley->description . '" onclick="WysiwygPluginSmileyPaste(this.title);"/>';
    }
  }

  $output .= $smiley_info;
  $output .= '<span class="close">' . t('Close') . '</span>';
  $output .= '</div>';
  return $output;
}
